package com.snopy.reference.weak;

import java.lang.ref.WeakReference;

/**
 * @author snopy
 * @version 1.0.0
 * @classname WeakReferenceDemo
 * @description jvm config -Xmx5m -Xms5m -verbose:gc -
 * @email 77912204@qq.com
 * @date 2022/03/16 0:42
 */
public class WeakReferenceDemo {
    public static void main(String[] args) {
        WeakReference<ClassMemory> m1 = new WeakReference<ClassMemory>(new ClassMemory());
        System.out.println("m1"+m1.get());
        //gc会清理弱引用
        System.gc();
        System.out.println("m1"+m1.get());
        WeakReference<ClassMemory> m2 = new WeakReference<ClassMemory>(new ClassMemory());
        System.out.println("m2"+m2.get());
        WeakReference<ClassMemory> m3 = new WeakReference<ClassMemory>(new ClassMemory());
        System.out.println("m3"+m3.get());
        WeakReference<ClassMemory> m4 = new WeakReference<ClassMemory>(new ClassMemory());
        System.out.println("m4"+m4.get());
        WeakReference<ClassMemory> m5 = new WeakReference<ClassMemory>(new ClassMemory());
        System.out.println("m5"+m5.get());
    }
}
/**
 * 模拟1M内存
 */
class ClassMemory{
    private final byte[] memory = new byte[1048576];
}
